/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.app.modular.dict.tag.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.app.modular.dict.tag.result.DictTagHobbyResult;
import vip.xiaonuo.app.modular.dict.tag.result.DictTagPersonalityResult;
import vip.xiaonuo.app.modular.dict.tag.result.DictTagResult;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.app.modular.dict.tag.entity.AppDictTag;
import vip.xiaonuo.app.modular.dict.tag.enums.AppDictTagExceptionEnum;
import vip.xiaonuo.app.modular.dict.tag.mapper.AppDictTagMapper;
import vip.xiaonuo.app.modular.dict.tag.param.AppDictTagParam;
import vip.xiaonuo.app.modular.dict.tag.service.AppDictTagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * app标签表service接口实现类
 *
 * @author 郑雄伟
 * @date 2023-09-23 10:30:10
 */
@Service
public class AppDictTagServiceImpl extends ServiceImpl<AppDictTagMapper, AppDictTag> implements AppDictTagService {


    @Override
    public List<AppDictTag> getDictTagHobbyList() {
        LambdaQueryWrapper<AppDictTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppDictTag::getType, 2);
        return this.list(queryWrapper);
    }

    @Override
    public List<AppDictTag> getDictTagPersonalityList() {
        LambdaQueryWrapper<AppDictTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppDictTag::getType, 1);
        return this.list(queryWrapper);
    }

    @Override
    public DictTagResult getDictTagResultList() {

        DictTagResult dictTagResult = new DictTagResult();
        List<DictTagHobbyResult> hobbyTagResults = new ArrayList<>();
        //爱好
        List<AppDictTag> dictTagHobbyList = this.getDictTagHobbyList();
        dictTagHobbyList.forEach(e -> {
            DictTagHobbyResult hobbyTagResult = new DictTagHobbyResult();
            BeanUtil.copyProperties(e, hobbyTagResult);
            hobbyTagResults.add(hobbyTagResult);
        });
        List<DictTagPersonalityResult> dictTagPersonalityResults = new ArrayList<>();
        //个性
        List<AppDictTag> dictTagPersonalityList = this.getDictTagPersonalityList();
        dictTagPersonalityList.forEach(e -> {
            DictTagPersonalityResult personalityTagResult = new DictTagPersonalityResult();
            BeanUtil.copyProperties(e, personalityTagResult);
            dictTagPersonalityResults.add(personalityTagResult);
        });
        dictTagResult.setDictTagHobbyResultList(hobbyTagResults);
        dictTagResult.setDictTagPersonalityResultList(dictTagPersonalityResults);

        return dictTagResult;
    }

    @Override
    public PageResult<AppDictTag> page(AppDictTagParam appDictTagParam) {
        QueryWrapper<AppDictTag> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(appDictTagParam)) {

            // 根据标签名字 查询
            if (ObjectUtil.isNotEmpty(appDictTagParam.getName())) {
                queryWrapper.lambda().eq(AppDictTag::getName, appDictTagParam.getName());
            }
            // 根据标签类型   1个性标签   2兴趣爱好  查询
            if (ObjectUtil.isNotEmpty(appDictTagParam.getType())) {
                queryWrapper.lambda().eq(AppDictTag::getType, appDictTagParam.getType());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<AppDictTag> list(AppDictTagParam appDictTagParam) {
        return this.list();
    }

    @Override
    public void add(AppDictTagParam appDictTagParam) {
        AppDictTag appDictTag = new AppDictTag();
        BeanUtil.copyProperties(appDictTagParam, appDictTag);
        this.save(appDictTag);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<AppDictTagParam> appDictTagParamList) {
        appDictTagParamList.forEach(appDictTagParam -> {
            this.removeById(appDictTagParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(AppDictTagParam appDictTagParam) {
        AppDictTag appDictTag = this.queryAppDictTag(appDictTagParam);
        BeanUtil.copyProperties(appDictTagParam, appDictTag);
        this.updateById(appDictTag);
    }

    @Override
    public AppDictTag detail(AppDictTagParam appDictTagParam) {
        return this.queryAppDictTag(appDictTagParam);
    }

    /**
     * 获取app标签表
     *
     * @author 郑雄伟
     * @date 2023-09-23 10:30:10
     */
    private AppDictTag queryAppDictTag(AppDictTagParam appDictTagParam) {
        AppDictTag appDictTag = this.getById(appDictTagParam.getId());
        if (ObjectUtil.isNull(appDictTag)) {
            throw new ServiceException(AppDictTagExceptionEnum.NOT_EXIST);
        }
        return appDictTag;
    }

    @Override
    public void export(AppDictTagParam appDictTagParam) {
        List<AppDictTag> list = this.list(appDictTagParam);
        PoiUtil.exportExcelWithStream("SnowyAppDictTag.xls", AppDictTag.class, list);
    }

}
